<?php
namespace app\model\crm;

use think\Model;

class Customer extends Model
{
    // 设置表名
    protected $name = 'crm_customer';
    
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    
    // 定义字段类型
    protected $type = [
        'status' => 'integer',
        'cooperation_time' => 'integer'
    ];
    
    // 关联联系人
    public function contacts()
    {
        return $this->hasMany(Contact::class, 'customer_id');
    }
    
    // 关联跟进记录
    public function followups()
    {
        return $this->hasMany(Followup::class, 'customer_id');
    }
    
    // 关联业务订单
    public function orders()
    {
        return $this->hasMany(Order::class, 'customer_id');
    }
    
    // 获取客户状态标签
    public function getStatusTextAttr($value, $data)
    {
        $status = [
            1 => '潜在客户',
            2 => '跟进中',
            3 => '已合作',
            4 => '公海客户',
            5 => '已流失'
        ];
        return $status[$data['status']] ?? '未知';
    }
    
    // 检查是否已合作
    public function getIsCooperatedAttr($value, $data)
    {
        return $data['status'] == 3;
    }
    
    // 检查是否为公海客户
    public function getIsPublicAttr($value, $data)
    {
        return $data['status'] == 4;
    }
    
    // 获取客户类型标签
    public function getTypeTextAttr($value, $data)
    {
        $type = [
            1 => '企业客户',
            2 => '个人客户',
            3 => '政府客户'
        ];
        return $type[$data['type']] ?? '未知';
    }
}